import { IFeidaoAiNodejsComponent } from '@dfeidao/atom-nodejs/interfaces';
import render from '@dfeidao/atom-nodejs/render/render';
import format from '@dfeidao/atom-web/dt/format';
import set from '@dfeidao/atom-web/global/set';

import nodejs from '@dfeidao/atom-web/msg/nodejs';
import p001 from './p001';
import p002 from './p002';
import p003 from './p003';
export default async function na001(fd: IFeidaoAiNodejsComponent) {
	// todo
	const users = { staff_no: "YG00000060", staff_name: "庄开局" };
	set('users', users);
	set('no', users.staff_no);
	set('staff_name', users.staff_name);
	const res1 = await nodejs<{ dt: Array<{ exam_id: string, length: number }> }>('employee-examination/zj-000001/s001', {});
	const res2 = await nodejs<{ dt: Array<{ exam_id: string, paper_number: string }> }>('employee-examination/zj-000001/s002',
		{
			filter: {
				candidate_no: users.staff_no
			}
		});
	const ret = new Array();
	for (let i = 0; i < res2.dt.length; i++) {
		ret[i] = res2.dt[i].exam_id;
	}
	// 数组去重
	const ses = new Set(ret);
	const newArr = Array.from(ses);
	const a = Array();
	for (let i = 0; i < res1.dt.length; i++) {
		a.push(res1.dt[i].exam_id);
	}
	const res3 = await nodejs<{
		dt: Array<{
			exam_id: string,
			exam_name: string,
			exam_time: string,
			examination_category: string,
			exam_score: string,
			exam_time1: string,
			exam_caozuo: string
		}>, c: number
	}>('employee-examination/zj-000001/s003',
		{
			filter: {
				pn: 1,
				exam_id: a
			}
		});
	const res4 = await nodejs<{ dt: Array<{ exam_id: string, exam_score: string, exam_time: number }> }>('employee-examination/zj-000001/s004',
		{
			filter: {
				exam_id: a
			}
		});
	// 转时间格式
	const p = res4.dt.map((n) => {
		return { ...n, exam_time: format(n.exam_time, 2) };
	});
	const dat = res3.dt.map((n) => {
		for (let index = 0; index < res4.dt.length; index++) {
			if (res4.dt[index].exam_id === n.exam_id) {
				n.exam_time1 = p[index].exam_time;
				n.exam_score = res4.dt[index].exam_score;
				return n;
			}
		}
		n.exam_time1 = '';
		n.exam_score = '';
		return n;
	});
	// 操作
	const da = dat.map((n) => {
		for (let index = 0; index < newArr.length; index++) {
			if (newArr[index] === n.exam_id) {
				n.exam_caozuo = "1";
				return n;
			}
		}
		n.exam_caozuo = "0";
		return n;
	});
	if (da.length > 0) {
		render(fd.data.node, da, p001, 'p001');
		if (res3.c > 20) {
			render(fd.data.node, res3.c, p003, 'p003');
		}
	} else {
		render(fd.data.node, '', p002, 'p002');
	}
}
